package com.person.title.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.title.model.Title;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleService;

public class TitleAction extends BaseAction {

	private TitleService titleService ;
	private List <Title> titles ;
	private Title title ;
	private String titleCode ;
	private String titleName ;
	private String titleImage ;
	private List <TitleSub> titleSubs ;
	private int flag = 0 ;
	
	private String image;
	private String imageFileName;
		
	
	
	/**
	 * 栏目管理的首页
	 * 左边显示所有的一级栏目
	 * 右边是栏目管理的说明
	 * @return
	 */
	public String index () {		
		return "index" ;		
	}
	
	
	public String left () {
		
		titles = this.titleService.titleList() ;
		return "left" ;
	}
	
	
	public String right () {
		
		return "right" ;
	}
	
	
	/**
	 * 转向添加页面
	 * @return 
	 */
	public String preCreate () {
		
		return "createTitle" ;
	}
	

	/**
	 * 添加一级栏目节点
	 * @return
	 */
	public String create(){
		String forward = "" ;		
		char state = this.titleService.createTitle(title) ;
		if(image!=null){
			title.setTitleImage(upload(image,imageFileName));
		}
		titleService.update(title);
		if(state == '1'){
			info = getText("title.create.success");	
			flag = 1 ;
			forward = "create_success";
		}else if(state == '2'){
			
			info = getText("title.code");
			forward = INPUT;
			
		}else if(state == '3'){
			
			info =  getText("title.name");
			forward = INPUT;
		}else if(state == '4'){
			
			info =  getText("title.nameEn");
			forward = INPUT;
		}
		
		return forward;
	}
	
	
	/**
	 * 转向修改页面
	 * @return 
	 */
	public String preModify () {
		
		title = this.titleService.findSysTitleId(title.getTitleId()) ;
		
		return "modifyTitle" ;
	}
	
	
	/**
	 * 修改一级栏目节点
	 * @return
	 */
	public String modify(){
		String forward = "" ;
		char state = this.titleService.modifyTitle(title) ;
		if(image!=null){
			title.setTitleImage(upload(image,imageFileName));
		}
		titleService.update(title);
		char state1 = this.titleService.modifyJuniorTitleSub(title, titleCode) ;
		
		if(state == '1' && state1=='1'){
			info = getText("title.modify.success");	
			flag = 1 ;
			forward = "modify_success";
		}else if(state == '2'){
			
			info = getText("title.code");
			forward = INPUT;
			
		}else if(state == '3'){
			
			info =  getText("title.name");
			forward = INPUT;
		}else if(state == '4'){
			
			info =  getText("title.nameEn");
			forward = INPUT;
		}
		
		return forward;
	}
	
	
	
	/**
	 * 删除一级栏目节点
	 * 
	 * @return
	 */
	public String delete() {
		String forward = "" ;
		
		if(title != null) {
			title = this.titleService.findSysTitleId(title.getTitleId()) ;
			char state = this.titleService.deleteTitle(title) ;
			if(state == '1') {
				info = getText("title.delete.success") ;
				flag = 1;
				forward = "delete_success" ;
			}else if(state == '0') {				
				info = getText("title.delete.fail");
				forward = "delete_success";
				
			}
		}
		return forward;
	}
	
	
	
	/**
	 * 管理一级节点下的子栏目节点
	 * @return
	 */
	public String manageSub() {
		title = titleService.findSysTitleId(title.getTitleId()) ;
				
		titleSubs = this.titleService.juniorTitleList(title.getTitleId()) ;
		
		titleImage = title.getTitleImage() ;
		
		titleCode = title.getTitleCode() ;
		
		titleName = title.getTitleName() ;
		
		return "manageSub" ;
	}
		
	public String upload(String image,String imageFileName) {
		
		String imageType = imageFileName.substring(
				imageFileName.lastIndexOf("."));
		String rootDir = request.getRealPath(File.separator)
				+ Constants.TITLE_DIR;
		String picName = String.valueOf(new Date().getTime());
		String imageDir = rootDir + picName + imageType;

		File rootFile = new File(rootDir);
		File imageFile = new File(imageDir);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}

		if (imageFile.exists()) {
			imageFile.delete();
		}

		try {
			FileInputStream is = new FileInputStream(image);
			FileOutputStream os = new FileOutputStream(imageDir);
			byte[] buffer = new byte[4096];
			int lenth = 0;
			while ((lenth = is.read(buffer)) > 0) {
				os.write(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String fileType=picName + imageType;
		return fileType;
	}
	public TitleService getTitleService() {
		return titleService;
	}


	public void setTitleService(TitleService titleService) {
		this.titleService = titleService;
	}


	public List<Title> getTitles() {
		return titles;
	}


	public void setTitles(List<Title> titles) {
		this.titles = titles;
	}


	public Title getTitle() {
		return title;
	}


	public void setTitle(Title title) {
		this.title = title;
	}


	public int getFlag() {
		return flag;
	}


	public void setFlag(int flag) {
		this.flag = flag;
	}


	public String getTitleCode() {
		return titleCode;
	}


	public void setTitleCode(String titleCode) {
		this.titleCode = titleCode;
	}


	public String getTitleImage() {
		return titleImage;
	}


	public void setTitleImage(String titleImage) {
		this.titleImage = titleImage;
	}


	public List<TitleSub> getTitleSubs() {
		return titleSubs;
	}


	public void setTitleSubs(List<TitleSub> titleSubs) {
		this.titleSubs = titleSubs;
	}


	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}


	public String getImage() {
		return image;
	}


	public void setImage(String image) {
		this.image = image;
	}


	public String getImageFileName() {
		return imageFileName;
	}


	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}


	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
